import json

from rest_framework.generics import ListAPIView, CreateAPIView
from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet

from question.serializers.questions import QuestionsIdLabelNewLabelsSerializer
from question.utils import get_user_dict
from roast.models import Spit
from roast.serializers.rolist import DebunkSerializer
from scf_project.utils.pagenum import PageNum


#
# class SpitView(ModelViewSet):
#     serializer_class = DebunkSerializer
#     queryset = Spit.objects.all()
#

# 收藏或取消收藏
class SpitFollowView(APIView):
    def put(self, request, id):
        # 获取吐槽对象
        spit = Spit.objects.get(id=id)
        # 获取收藏状态
        collected = spit.collected
        # print(type(collected))
        # 判断是否收藏
        if collected == 'True':
            # 取消收藏
            spit.collected = False
            spit.save()
            return Response({'message': '取消成功',
                             'success': False})
        else:
            # 收藏成功
            spit.collected = True
            spit.save()
            return Response({'message': '收藏成功',
                             'success': True})


# 点赞与取消点赞
class SpitHasthumbupView(APIView):
    def put(self, request, id):
        # 获取吐槽对象
        spit = Spit.objects.get(id=id)
        # 获取点赞状态
        hasthumbup = spit.hasthumbup
        if hasthumbup == 'Ture':
            spit.hasthumbup = False
            thumbup = spit.thumbup
            if thumbup <= 0:
                return
            thumbup -= 1
            spit.thumbup = thumbup
            spit.save()
            return Response({
                'message': '取消成功',
                'success': False})
        else:
            spit.hasthumbup = True
            thumbup = spit.thumbup
            if not thumbup:
                spit.thumbup = 0

            spit.thumbup += 1
            spit.save()
            return Response({'message': '点赞成功',
                             'success': True})


# 吐槽列表和发布吐槽
class SpitView(APIView):
    def post(self, request):
        """发布吐槽"""

        # 接收参数
        json_data = request.body.decode()
        data = json.loads(json_data)
        content = data.get('content')
        parent_id = data.get('parent')
        # 获取用户对象
        # user = request.user
        # user_dict = get_user_dict(request)
        # user_id = user_dict['user_id']
        # 创建新的吐槽
        try:
            # spit = Spit(content=content, user_id=user_id, parent_id=parent_id)
            spit = Spit.objects.create(content=content, user_id=1, parent_id=parent_id)

        except Exception as e:
            return Response({'message': '吐槽失败'})

        serializer = DebunkSerializer(spit)
        return Response(serializer.data)

    def get(self, request):
        # 吐槽列表

        # 获取所有吐槽对象
        spits = Spit.objects.all()
        page = PageNumberPagination()
        page.page_size = 10
        page.page_query_param = 'page'
        page.page_size_query_param = 10
        page.max_page_size = 10
        ret = page.paginate_queryset(spits, request)
        serializer = DebunkSerializer(instance=ret, many=True)
        return Response(serializer.data)


# 吐槽详情
class SpitDetailView(APIView):
    def get(self, request, id):
        # 获取吐槽对象
        spit = Spit.objects.get(id=id)
        serializer = DebunkSerializer(spit)
        return Response(serializer.data)


# 获取吐槽评论
class SpitCommentView(APIView):
    def get(self, request, id):
        # 获取吐槽对象集
        spits = Spit.objects.filter(parent_id=id)
        serializer = DebunkSerializer(spits,many=True)
        return Response(serializer.data)

